package com.ncloud.resman.mapper;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.resman.entity.VirtualMachine;
import org.apache.ibatis.annotations.*;

import java.util.List;


/**
 * Created by shiqiang on 2017/8/14.
 */
@Mapper
public interface VirtualMachineMapper {
    @Insert("insert into res_virtual_machine( virtual_id, device_id, virtual_host_name, os_name, cpu_num, memory_capacity," +
            " state, create_user, create_date, update_user, update_date)"
            + " values(#{virtualId},#{deviceId},#{virtualHostName},#{osName},#{cpuNum},#{memoryCapacity},#{state},"
            + "#{createUser},#{createDate},#{updateUser},#{updateDate})")
    int insertVirtualMachine(VirtualMachine virtualMachine);
    @Update("update res_virtual_machine set virtual_host_name = #{virtualHostName},os_name=#{osName},cpu_num =#{cpuNum},memory_capacity =#{memoryCapacity}," +
            " update_user=#{updateUser},update_date=#{updateDate} where virtual_id =#{virtualId}")
    int updateVirtualMachine(VirtualMachine virtualMachine);

    /*修改虚拟机状态*/
    @Update("update res_virtual_machine set state = #{1} where virtual_id =#{0}")
    int updateVirtualMachineState(Long virtualId,String state);
    /*删除*/
    @Delete(value = "delete from res_virtual_machine WHERE virtual_id = #{0} and state='4'")
    int deleteVirtualMachine(Long virtualId);
    @Select("SELECT virtual_id, device_id, virtual_host_name, os_name, cpu_num, memory_capacity, state "
            + "FROM res_virtual_machine where virtual_id = #{virtualId}")
    @Results({
            @Result(column="virtual_id",property="virtualId"),@Result(column="device_id",property="deviceId"),
            @Result(column="virtual_host_name",property="virtualHostName"),@Result(column="os_name",property="osName"),
            @Result(column="cpu_num",property="cpuNum"),@Result(column="memory_capacity",property="memoryCapacity"),
            @Result(column="state",property="state")})
    VirtualMachine getVirtualMachineById(Long virtualId);//
    /*获取主机下虚拟机信息*/
    @Select("SELECT virtual_id, device_id, virtual_host_name, os_name, cpu_num, memory_capacity, state "
            + " FROM res_virtual_machine where device_id = #{deviceId}")
    @Results({
            @Result(column="virtual_id",property="virtualId"),@Result(column="device_id",property="deviceId"),
            @Result(column="virtual_host_name",property="virtualHostName"),@Result(column="os_name",property="osName"),
            @Result(column="cpu_num",property="cpuNum"),@Result(column="memory_capacity",property="memoryCapacity"),
            @Result(column="state",property="state")})
    List<VirtualMachine> getVirtualMachineByDeviceId(Long deviceId);//


    /*获取所有机房信息*/
    @Select("SELECT room_id, room_name, area_name, country_name, province_name, city_name, addr"
            + " FROM sys_computer_room where room_state = '1'")
    @Results({
            @Result(column="room_id",property="roomId"),@Result(column="room_name",property="roomName"),
            @Result(column="area_name",property="areaName"),@Result(column="country_name",property="countryName"),
            @Result(column="province_name",property="provinceName"),@Result(column="city_name",property="cityName"),
            @Result(column="addr",property="addr")})
    List<JSONObject> getRoomAll();//

    /*根据机房查询其下对应的机柜*/
    @Select("SELECT cabinet_id, cabinet_code, room_id, place_num, state FROM res_cabinet" +
            " where room_id=#{0} and state=1 ")
    @Results({
            @Result(column="cabinet_id",property="cabinetId"),@Result(column="cabinet_code",property="cabinetCode"),
            @Result(column="room_id",property="roomId"),@Result(column="place_num",property="placeNum"),
            @Result(column="state",property="state")})
    List<JSONObject> getCabinetByRoomId(Long roomId);//

    /*查询机柜下的所有服务器*/
    @Select(value = { "select c.device_id,c.ipmi_addr from res_device_location a,res_device_base b ,res_device_server c "
            + " where a.device_id=b.device_id and b.device_id = c.device_id and b.state='1' and  a.cabinet_id=#{0} "
    })
    @Results({
            @Result(javaType = String.class,column = "device_id", property = "deviceId"),
            @Result(column = "ipmi_addr", property = "ipmiAddr")
    })
    List<JSONObject> getDeviceServerByCabinetId(Long cabinetId);

     /*查询服务器的基本信息*/
     @Select(value = { "SELECT a.device_id ,a.room_id ,a.room_name,a.cabinet_id,a.cabinet_code,a.start_num,a.end_num,a.sn_num,b.ipmi_addr  "
             + " FROM res_device_location a,res_device_server b,res_device_base c  where a.device_id = b.device_id  " +
             " and b.device_id = c.device_id and c.state='1' and  a.device_id=#{0} "
     })
     @Results({
             @Result(javaType = String.class,column = "device_id", property = "deviceId"),
             @Result(javaType = String.class,column = "room_id", property = "roomId"),
             @Result(column = "room_name", property = "roomName"),
             @Result(javaType = String.class,column = "cabinet_id", property = "cabinetId"),
             @Result(column = "cabinet_code", property = "cabinetCode"),
             @Result(column = "start_num", property = "startNum"),
             @Result(column = "end_num", property = "endNum"),
             @Result(column = "sn_num", property = "snNum"),
             @Result(column = "ipmi_addr", property = "ipmiAddr")
     })
     JSONObject getDeviceServerBaseInfo(Long deviceId);

}
